package com.jck.wx.web;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jck.dplus.common.annomapper.MybatisBaseDaoCRUD;

import lombok.extern.slf4j.Slf4j;


@Slf4j
@Controller
public class MyController {

	@Autowired
	public MybatisBaseDaoCRUD mBaseDao;


	@RequestMapping("MP_verify_awgwdgqh4Eu7vh95")
    public String downLoad(HttpServletResponse response){
        String filename="MP_verify_awgwdgqh4Eu7vh95.txt";
        String filePath = "D:\\eclipse-workspace\\jck-wx-web\\src\\main\\resources\\views\\MP_verify_awgwdgqh4Eu7vh95.txt" ;
        File file = new File(filePath + "/" + filename);
        if(file.exists()){ //判断文件父目录是否存在
            response.setContentType("application/force-download");
            response.setHeader("Content-Disposition", "attachment;fileName=" + filename);
            
            byte[] buffer = new byte[1024];
            FileInputStream fis = null; //文件输入流
            BufferedInputStream bis = null;
            
            OutputStream os = null; //输出流
            try {
                os = response.getOutputStream();
                fis = new FileInputStream(file); 
                bis = new BufferedInputStream(fis);
                int i = bis.read(buffer);
                while(i != -1){
                    os.write(buffer);
                    i = bis.read(buffer);
                }
                
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("----------file download" + filename);
            try {
                bis.close();
                fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return null;
    }

}